package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.QuestionDao;
import com.hqyj.pojo.Question;
import com.hqyj.pojo.ResultVo;
import com.hqyj.service.QuestionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Autor 伍军
 * @Date 2022/9/26 10:28
 * @Version 1.0
 **/
@Service
public class QuestionServiceImpl implements QuestionService {
    //注入Maper
    //Resource 和 autiwird的区别：自动装配方式不相同，Resource根据名称装配，autiwird 根据类型装配
    @Resource
    QuestionDao questionDao;
    @Override
    public ResultVo queryPageByCondition(Question q) {
        //1 创建分页对象
        Page<Question> page = new Page<>(q.getPage(),q.getRow());
        //创建条件构造器
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        //创建时间的倒序
        queryWrapper.orderByDesc("create_time");
        //条件查询-题目的模糊查询
        queryWrapper.like(q.getQuContent()!=null&&!q.getQuContent().equals(""),"qu_content",q.getQuContent());
        //条件查询-题目类型的精确查询
        queryWrapper.eq(q.getQuType()!=null&&!q.getQuType().equals(""),"qu_type",q.getQuType());

        //2 查询分页
        Page<Question> pageList = questionDao.selectPage(page,queryWrapper);
        ResultVo rv = new ResultVo();
        if(pageList.getRecords().size()==0){
            rv.setInfo("没有查询到数据");
            return rv;
        }
        Map<String,Object> map = new HashMap<>();
        //当前页集合
        map.put("list",pageList.getRecords());
        //总页数
        map.put("totalPage",pageList.getPages());
        //总条数
        map.put("total",pageList.getTotal());
        rv.setInfo("有数据");
        rv.setSuccess(true);
        rv.setData(map);
        return rv;
    }
}
